﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Windows.Browser;

namespace OntoWPFSilverlight.Widget
{
    [OntoWPFSilverlight.Annotation.Widget("Slider")]
    public class Slider : WidgetCanvas, Interfaces.IWidgetWithItems
    {

        Groupbox.GroupBox gr = null;
        public System.Windows.Controls.Slider sli;
        //CustomSlider sli;

        public TextBox text = new TextBox();

        public DockPanel dock;

        public double TickFrequency { get; set; }
        public Canvas bottomTicksCanvas;

        public DataTemplate TickTemplate
        {
            get { return (DataTemplate)GetValue(TickTemplateProperty); }
            set { SetValue(TickTemplateProperty, value); }
        }

        public static readonly DependencyProperty TickTemplateProperty =
            DependencyProperty.Register("TickTemplate", typeof(DataTemplate), typeof(CustomSlider), null);



        public Slider()
        {
            //System.Diagnostics.Debug.WriteLine("slider+++++++++++++++++++++++++++++++++++++++++++");
            
           //DefaultStyleKey = typeof(CustomSlider);
            Build();
        }

        private void Build()
        {
            sli = new System.Windows.Controls.Slider();
            gr = new Groupbox.GroupBox();
            bottomTicksCanvas = new Canvas();
            dock = new DockPanel();
            dock.LastChildFill = true;
            //DockPanel.SetDock(bottomTicksCanvas, Dock.Top);
            //DockPanel.SetDock(sli, Dock.Top);
            //dock.Children.Add(sli);
            //dock.Children.Add(bottomTicksCanvas);
            
            gr.Header = "Label";
            //sli = new System.Windows.Controls.Slider();
            //sli.Maximum = 
            //sli = new CustomSlider();
            //sli.Minimum = 1;
            //sli.Maximum = this.ontologiesNodes.Nodes.Count;

            gr.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            //sli.HorizontalAlignment = HorizontalAlignment.Stretch;
            //NUMERO FIGLI
            //sli.TickFrequency = this.ontologiesNodes.Nodes.Count;
            //gr.Content = sli;
            
            
            //this.Children.Add(gr);
            this.Children.Add(gr);
        }

        internal void AddItem(IEnumerable<XElement> nodes)
        {
            throw new NotImplementedException();
        }


        public override string Text
        {
            get
            {
                return gr.Header.ToString();
            }
            set
            {
                gr.Header = value;
            }
        }

        #region IWidgetWithItems Membri di

        OntoWPFSilverlight.Node ontologyNode;

        public OntoWPFSilverlight.Node Node
        {
            get
            {
                return ontologyNode;
            }
            set
            {
                ontologyNode = value;
            }
        }

        OntoWPFSilverlight.Xml.OntologiesNodes ontologiesNodes = null;
        public OntoWPFSilverlight.Xml.OntologiesNodes OntologiesNodes
        {
            get
            {
                return ontologiesNodes;
            }
            set
            {
                this.ontologiesNodes = value;
            }
        }

        public void update(List<Node> nodes)
        {
                       
            sli.HorizontalAlignment = HorizontalAlignment.Stretch;
            sli.Minimum = 1;
            sli.Maximum = nodes.Count;
            
            sli.ValueChanged += new RoutedPropertyChangedEventHandler<double>(sli_ValueChanged);
            TickFrequency = 1;
            string[] testo = new string[nodes.Count];
            foreach (Node _node in nodes) 
            {
                testo[nodes.IndexOf(_node)] = _node.Alias;
            }

            gr.Content = dock;
            DockPanel.SetDock(bottomTicksCanvas, Dock.Top);
            DockPanel.SetDock(sli, Dock.Top);
            dock.Children.Add(sli);
            dock.Children.Add(bottomTicksCanvas);
            //dock.Children.Add(text);
            dock.Margin = new Thickness(20, 0, 20, 0);
            
            
            Create_Ticks(testo);
        }

        void sli_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            text.Text = sli.Value.ToString();
        }

        public void update()
        {
            update(this.ontologiesNodes.Nodes);
        }

        #endregion

        #region CustomSlider

        public void Create_Ticks(string[] testo)
        {
            //bottomTicksCanvas.Children.Clear();
            int numberOfTicks = (int)((sli.Maximum - sli.Minimum + 1) / TickFrequency);
            //numberOfTicks++;
            for (int i = 0; i < numberOfTicks; i++)
            {
                double x1 = 5 + ((i) * ((this.WidgetSize.Width-62) / (numberOfTicks-1)));
                //try
                //{
                //    double a = sli.Width;
                //    double b = gr.Width;
                //    double c = dock.Width;
                //}
                //catch (Exception)
                //{
                    
                //    throw;
                //}
                
                
                bottomTicksCanvas.Children.Add(CreateTick(new Point(x1, 5), new Point(x1, 10)));
                //bottomTicksCanvas.Children.Add(CreateLabel((Math.Round(((Maximum - Minimum) / numberOfTicks) * (i), 0)).ToString(), 6.0, x1));                                     
                bottomTicksCanvas.Children.Add(CreateLabel(testo[i], 11.0, x1));
            }
        }

        TextBlock CreateLabel(string text, double top, double left)
        {
            TextBlock txt = new TextBlock();
            txt.Text = text;
            
            txt.SetValue(Canvas.TopProperty, top);
            txt.SetValue(Canvas.LeftProperty, left - txt.ActualWidth / 2);
            
            return txt;
        }

        FrameworkElement CreateTick(Point start, Point end)
        {
            if (TickTemplate == null)
            {
                Line ln = new Line();
                ln.Stroke = new SolidColorBrush(Colors.Blue);
                ln.StrokeThickness = 1.0;
                ln.X1 = start.X;
                ln.Y1 = start.Y;
                ln.X2 = end.X;
                ln.Y2 = end.Y;
                return ln;
            }
            else
            {
                ContentPresenter cp = new ContentPresenter();
                cp.Content = "a";
                cp.ContentTemplate = TickTemplate;
                cp.SetValue(Canvas.TopProperty, start.Y);
                cp.SetValue(Canvas.LeftProperty, start.X - (cp.ActualWidth / 2));
                return cp;
            }

        }

        

        #endregion


    }
}
